package UF_JNDI;

public class Example_02 {
    public void scan(Name name, String... filters) {
        try {
            DirContext ctx = initContext();
            try {
                for (final String filter : filters) {
                    search(name, filter, ctx);
                }
            } finally {
                ctx.close();
            }
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    private DirContext initContext() throws NamingException {
        Hashtable env = new Hashtable(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap:///o=JndiSample,dc=example,dc=com");

        DirContext ctx = new InitialDirContext(env);
        return ctx;
    }

    private void search(Name name, String filter, DirContext ctx) throws NamingException {
        final SearchControls searchControls = new SearchControls();
        searchControls.setReturningAttributes(necessaryItemAttributes);
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        final NamingEnumeration<SearchResult> resultNamingEnumeration = ctx.search(name, filter, searchControls);
    }
}
